package com.example.myopencv;


import android.Manifest;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myopencv.databinding.ActivityMainopenBinding;

import java.io.File;
import java.io.IOException;

public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback, Camera
        .PreviewCallback {

    static {
        System.loadLibrary("myopencv");
    }

    private ActivityMainopenBinding binding;

    private CameraHelper cameraHelper;
    TextView tvPermiss;
    TextView tvStart;
    TextView tvStart2;
    SurfaceView surfaceView;
    int cameraId = Camera.CameraInfo.CAMERA_FACING_FRONT;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainopenBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        tvStart = findViewById(R.id.tvStart);
        tvStart2 = findViewById(R.id.tvStart2);
        tvPermiss = findViewById(R.id.tvPermiss);
        surfaceView = findViewById(R.id.surfaceView);

        surfaceView.getHolder().addCallback(this);
        cameraHelper = new CameraHelper(cameraId);
        cameraHelper.setPreviewCallback(this);


        tvPermiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkPermission();

            }
        });


        tvStart2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {



            }
        });

    }

    public boolean checkPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(
                Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.CAMERA
            }, 1);

        }
        return false;
    }

    @Override
    protected void onResume() {
        super.onResume();

        cameraHelper.startPreview();
    }

    @Override
    protected void onStop() {
        super.onStop();
        //释放跟踪器
//        release();
        cameraHelper.stopPreview();
    }


    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        if (null != cameraHelper && null != cameraHelper.getmCamera()) {
            try {
                cameraHelper.getmCamera().setPreviewDisplay(holder);

            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        Log.e("6666", "=========== surfaceChanged: ");
        //设置surface 用于显示
        setSurface(holder.getSurface());
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }



    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {
        //传输数据


    }

    /**
     * 初始化 追踪器
     *
     * @param model
     */
    native void init(String model);

    /**
     * 设置画布
     * ANativeWindow
     *
     * @param surface
     */
    native void setSurface(Surface surface);

    /**
     * 处理摄像头数据
     *
     * @param data
     * @param w
     * @param h
     * @param cameraId
     */
    native void postData(byte[] data, int w, int h, int cameraId);

    /**
     * 释放
     */
//    native void release();
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            cameraHelper.switchCamera();
            cameraId = cameraHelper.getCameraId();
        }
        return super.onTouchEvent(event);
    }

}

